package managedbean;

import java.io.FileInputStream;
import java.io.IOException;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import javax.faces.bean.SessionScoped;
/**
 *
 * @author thanhle
 */
@ManagedBean(name = "download")
@SessionScoped
public class Download {
    @ManagedProperty(value="#{jasper}")
    private Jasper jasper;

    public Jasper getJasper() {
        return jasper;
    }

    public void setJasper(Jasper jasper) {
        this.jasper = jasper;
    }

   

    private StreamedContent file;



    public StreamedContent getFile() {
    
        try {
      
            String fileName =  jasper.getFileName();
         String path = jasper.getPath();
              if(path==null || path.isEmpty()){
            System.out.println("path unspecified");
              }
            file = new DefaultStreamedContent(new FileInputStream(
                   path),
                    "application/pdf", fileName);
        
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    public void setFile(StreamedContent file) {
        this.file = file;
    }
  
}
